//
//  NSDate+CLExtension.h
//
//
//  Created by joe on 10-6-12.
//  Copyright 2010 . All rights reserved.
//

#import "NSDate+CLExtension.h"
#import "CLDateCompare.h"
#import "NSDate+CLCalendar.h"


@implementation NSDate(RLExtend)

- (NSInteger)second
{
    return [self calendarUnit:NSCalendarUnitSecond];
}

- (NSInteger)minute
{
    return [self calendarUnit:NSCalendarUnitMinute];
}
-(NSInteger)hour
{
    return [self calendarUnit:NSCalendarUnitHour];
}
-(NSInteger)day
{
    return [self calendarUnit:NSCalendarUnitDay];
}
-(NSInteger)month
{
    return [self calendarUnit:NSCalendarUnitMonth];
}
-(NSInteger)year
{
    return [self calendarUnit:NSCalendarUnitYear];
}
-(NSInteger)weekDay
{
    return [self calendarUnit:NSCalendarUnitWeekday];
}

-(NSInteger)weekOfMonth
{
    return [self calendarUnit:NSCalendarUnitWeekOfMonth];
}
-(NSInteger)weekOfYear
{
    return [self calendarUnit:NSCalendarUnitWeekOfYear];
}

-(NSInteger)calendarUnit:(NSUInteger)unit
{
    NSCalendar* chineseClendar1 = [NSDate cl_Calendar];
    
    NSUInteger unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitWeekday | NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekOfYear;
    NSDateComponents *cps1 = [chineseClendar1 components:unitFlags fromDate:self];
    
    switch (unit) {
        case NSCalendarUnitSecond:
            return [cps1 second];
            
        case NSCalendarUnitMinute:
            return [cps1 minute];
            
        case NSCalendarUnitHour:
            return [cps1 hour];
            
        case NSCalendarUnitDay:
            return [cps1 day];
            
        case NSCalendarUnitMonth:
            return [cps1 month];
            
        case NSCalendarUnitYear:
            return [cps1 year];
            
        case NSCalendarUnitWeekday:
            return [cps1 weekday];
            
        case NSCalendarUnitWeekOfMonth:
            return [cps1 weekOfMonth];
            
        case NSCalendarUnitWeekOfYear:
            return [cps1 weekOfYear];
    }
    return 0;
}

/// 指定年月日时分秒生成date
+ (instancetype)dateByYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day hour:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second
{
    NSCalendar* calendar = [NSDate cl_Calendar];
    
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.year = year;
    components.month = month;
    components.day = day;
    components.hour = hour;
    components.minute = minute;
    components.second = second;
    
    return [calendar dateFromComponents:components];
}

- (NSDate*)monthStartDate
{
    return [NSDate dateByYear:self.year month:self.month day:1 hour:0 minute:0 second:0];
}

- (NSDate*)yearStartDate
{
    return [NSDate dateByYear:self.year month:0 day:0 hour:0 minute:0 second:0];
}

- (NSDate*)weekStartDate
{
    // 1(星期天) 2(星期1) 3(星期2) 4(星期3) 5(星期4) 6(星期5) 7(星期6)
    NSInteger weekDay = self.weekDay;
    NSInteger dif = 0;
    if (weekDay==1) {
        dif=6;
    } else
        dif = weekDay-1-1;
    
    return [[NSDate cl_Calendar] startOfDayForDate:[self addDay:-dif]];
}

- (NSDate*)weekEndDate
{
    // 1(星期天) 2(星期1) 3(星期2) 4(星期3) 5(星期4) 6(星期5) 7(星期6)
    NSInteger weekDay = self.weekDay;
    NSInteger dif = 0;
    if (weekDay==1) {
        dif = 0;
    } else
        dif = 7-weekDay+1;
    
    NSDate *weekEndDate = [self addDay:dif];
    
    return [NSDate dateByYear:weekEndDate.year month:weekEndDate.month day:weekEndDate.day hour:23 minute:59 second:59];
}

+ (NSDate*)startOfToDay
{
    return [[NSDate cl_Calendar] startOfDayForDate:[NSDate date]];
}

///// 获取本地时间
//+ (NSDate *)localDate
//{
//    NSDate *date = [NSDate date];
//    NSTimeZone *zone = [NSTimeZone systemTimeZone];
//    NSInteger interval = [zone secondsFromGMTForDate:date];
//    NSDate *localDate = [date dateByAddingTimeInterval:interval];
//    return localDate;
//}

@end





